﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Net;

namespace SendMail
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            if (dfsFrom.Text != dfsconfirmemail.Text)
            {
                lblError.Text = "Contact email and confirm email should match, please make sure both values are the same, thank you.";
                return;
            }

            try
            {
                string additionalInfo = "Phone: " + dfsphone.Text + "," + "Prefered Language: " + dfslanguage.Text +
                        ", Interest: " + dfcInterest.Text;
                var ms = new MailMessage(dfsFrom.Text, "contact@huishmarine.com")
                                     {
                                         Subject =
                                             ("Email from: " + dfstitle.Text + " " + dfsname.Text + " " +
                                              dfsLastName.Text + "  Email from:  " + dfsFrom.Text ),
                                         Body = (additionalInfo + "\n" + dfsBody.Text),
                                         ReplyTo = new MailAddress(dfsFrom.Text),
                                         IsBodyHtml = false
                                         
                                     };

                ms.From = new MailAddress(dfsFrom.Text);
                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587)
                                            {
                                                EnableSsl = true,
                                                UseDefaultCredentials = false,
                                                Credentials = new NetworkCredential("aliaymanmailer@gmail.com", "paris123")
                                            };

                smtpClient.Send(ms);
                Response.Redirect("thankyou.aspx");
            }
            catch (Exception ex)
            {
                string message = "";
                ConcatenateError(ex, ref message);
                lblError.Text = message;
            }


        }

        private void  ConcatenateError(Exception ex, ref string Message )
        {
            Message += ex.Message;
            if (ex.InnerException != null)
            {
                ConcatenateError(ex.InnerException, ref Message);
            }
        }
    }
}
